/**
 * The details view.
 *
 * (c) Copyright 2011 Nokia Corporation. All rights reserved.
 */

import Qt 4.7

import "../script/ajax.js" as AjaxScript
import "../script/common.js" as CommonScript
import "../lib/components"

Flickable {
    id: detailsViewContainer;
    x: 10;
    y: topBar.height + 5;
    width: parent.width - 20;  // 2 * 10 px margins
    height: parent.height - topBar.height - 5;
    contentWidth: width;
    contentHeight: betaTitle.height + 5 + betaImg.height + 5
                   + betaOverview.height;
    flickableDirection: "VerticalFlick";

    // The ID of the Beta currently on the screen
    property variant betaId;
    // Pre-fetched download information
    property variant downloadInfo;
    // The download info view inside of this details view.
    property variant downloadInfoViewObject;
    // The review view inside of this details view.
    property variant reviewViewObject;

    Behavior on opacity {
        PropertyAnimation {
            duration: 500;
        }
    }

    Connections {
        target: btnExitBack;
        // Called when the Back button is clicked.
        onButtonClicked: {
            // We're only interested in back to beta list or back to search
            // view functionality here.
            if (btnExitBack.backFunctionality ==
                    CommonScript.BACK_TO_BETA_LIST) {
                // Display the exit button.
                btnExitBack.backFunctionality = CommonScript.EXIT;

                // Destroy the details view and show the tabs.
                detailsViewContainer.destroy();
                tabs.opacity = 1;
            } else if (btnExitBack.backFunctionality ==
                       CommonScript.BACK_TO_SEARCH_VIEW) {
                // Display the exit button.
                btnExitBack.backFunctionality = CommonScript.EXIT;

                // Destroy the details view and show the search view.
                detailsViewContainer.destroy();
                searchViewContainer.betaViewObject.destroy();
                if (freeText.text.length == 0) {
                    freeText.text = CommonScript.FREE_TEXT;
                }
                freeTextArea.opacity = 1;
                btnToggleCategories.opacity = 1;
                categoryView.opacity = 1;
                tabs.opacity = 1;
            }
        }
    }

    Text {
        id: errorTextDetailsView;
        width: parent.width;
        text: "Unable to fetch details. Ensure that your device is connected to the Internet.";
        opacity: 0;
        wrapMode: "WordWrap";
    }

    Text {
        id: betaTitle;
        x: 0;
        y: 0;
        width: detailsViewContainer.width;
        font.pointSize: 10;
        horizontalAlignment: "AlignHCenter";
        wrapMode: "WordWrap";
    }

    Image {
        id: betaImg;
        anchors.top: betaTitle.bottom;
        anchors.topMargin: 5;
        width: 150;
        height: 150;
    }

    Button {
        id: btnDownload;
        anchors.top: betaTitle.bottom;
        anchors.left: betaImg.right;
        anchors.topMargin: 5;
        anchors.leftMargin: 20;
        width: parent.width / 2.5;
        text: "Download";
        opacity: 0;

        onClicked: {
            showDownloadInfoView();
        }
    }

    Button {
        id: btnViewReviews;
        anchors.top: btnDownload.bottom;
        anchors.left: betaImg.right;
        anchors.topMargin: 5;
        anchors.leftMargin: 20;
        width: parent.width / 2.5;
        text: "View Reviews";
        opacity: 0;

        onClicked: {
            showReviewView();
        }
    }

    Text {
        id: betaRating;
        anchors.top: btnViewReviews.bottom;
        anchors.left: betaImg.right;
        anchors.topMargin: 5;
        anchors.leftMargin: 20;
    }

    Text {
        id: betaOverview;
        anchors.top: betaImg.bottom;
        anchors.topMargin: 5;
        width: detailsViewContainer.width;
        wrapMode: "WordWrap";
    }

    function fetchDetails(betaId) {
        var req = AjaxScript.createXMLHttpRequest();

        if (req) {
            var url = CommonScript.BETA_LABS_API_URL + "/application/" + betaId;
            AjaxScript.loadXMLDoc(req, url, cbFetchDetails);
        }
    }

    // Called when the server responds to the fetch details call
    function cbFetchDetails(xml, text) {
        if (busyIndicator) {
            busyIndicator.opacity = 0;
        }

        if (xml == null) {
            errorTextDetailsView.opacity = 1;
            return;
        }

        errorTextDetailsView.opacity = 0;

        betaId = CommonScript.getElementsByTagName(
                xml.documentElement, "id")[0].firstChild.nodeValue;

        var title = CommonScript.getElementsByTagName(
                xml.documentElement, "title")[0].firstChild.nodeValue;
        betaTitle.text = title;

        var imgSrc = CommonScript.getElementsByTagName(
                xml.documentElement, "image")[0].firstChild.nodeValue;
        betaImg.source = imgSrc;

        btnDownload.opacity = 1;
        btnViewReviews.opacity = 1;

        var rating = CommonScript.getElementsByTagName(
                xml.documentElement, "rating")[0].firstChild.nodeValue;
        betaRating.text = "Rating: " + Math.round(rating);

        var overview = CommonScript.getElementsByTagName(
                xml.documentElement, "overview")[0].firstChild.nodeValue;

        // Remove objects from the HTML code.
        var objectStartInd;
        while ((objectStartInd = overview.indexOf("<object")) != -1) {
            var objectEndInd = overview.indexOf("</object>", objectStartInd)
                + "</object>".length;
            overview = overview.substring(0, objectStartInd)
                    + overview.substring(objectEndInd);
        }

        betaOverview.text = overview;

        // Load the download information into a variable even though the
        // download info view has not been activated yet. This saves bandwidth,
        // because no HTTP request is needed to fetch this same information
        // later.
        var downloadNode = CommonScript.getElementsByTagName(
                xml.documentElement, "download")[0];
        // Download info can be empty. In this case, disable the download button.
        if (downloadNode.firstChild == undefined) {
            btnDownload.enabled = false;
        } else {
            downloadInfo = downloadNode.firstChild.nodeValue;
        }
    }

    function showDownloadInfoView() {
        var downloadInfoViewComponent =
            Qt.createComponent("DownloadInfoView.qml");
        if (downloadInfoViewComponent.status == Component.Ready) {
            // Back button displays the details view.
            btnExitBack.backFunctionality = CommonScript.BACK_TO_DETAILS_VIEW;

            // Hide the details view.
            detailsViewContainer.opacity = 0;

            downloadInfoViewObject =
                downloadInfoViewComponent.createObject(backgroundImage);

            downloadInfoViewObject.showDownloadInfo(downloadInfo);
        }
    }

    function showReviewView() {
        var reviewViewComponent =
            Qt.createComponent("ReviewView.qml");
        if (reviewViewComponent.status == Component.Ready) {
            busyIndicator.opacity = 1;

            // Back button displays the details view.
            btnExitBack.backFunctionality = CommonScript.BACK_TO_DETAILS_VIEW;

            // Hide the details view.
            detailsViewContainer.opacity = 0;

            reviewViewObject =
                reviewViewComponent.createObject(backgroundImage);

            reviewViewObject.showReviews(betaId);
        }
    }
}
